package Map.Test;

/*
 * 需求：随机发牌，按顺序显示牌
 *
 * */

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class 斗地主顺序发牌 {
    public static void main(String[] args) {
        //定义花色、牌面集合,并添加元素
        List<String> color = List.of("♥️", "♦️", "♣️", "♠️");
        List<String> number = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
        //定义一个数组存放所有的牌面
        ArrayList<String> poker = new ArrayList();
        //定义一个集合存放牌的索引,索引为1-54
        ArrayList<Integer> index = new ArrayList<>();
        for (int i = 0; i < 54; i++) {
            index.add(i);
        }
        //System.out.println(index);
        //定义一个map用来存放所有牌面及其索引号（区分牌的大小）
        HashMap<Integer, String> map = new HashMap<>();
        //放入大小王
        map.put(0, "大王");
        poker.add("大王");
        poker.add("小王");
        map.put(1, "小王");
        int count = 2;
        //使用循环将其他牌放入
        for (String s : number) {
            for (String s1 : color) {
                poker.add(s1 + s);
                map.put(count, s1 + s);
                count++;
            }
        }
        //增强for代码简洁
        // for (int i = 0; i < number.size(); i++) {
        //     for (int j = 0; j < color.size(); j++) {
        //         poker.add(color.get(j) + number.get(i));
        //         map.put(count, (color.get(j) + number.get(i)));
        //         count++;
        //     }
        // }

        //System.out.print(poker);
        //System.out.println();
        //System.out.println(map);
        //定义三个玩家和一个底牌集合
        ArrayList one = new ArrayList<>();
        ArrayList two = new ArrayList<>();
        ArrayList three = new ArrayList<>();
        ArrayList dipai = new ArrayList<>();

        //打乱索引
        Collections.shuffle(index);
        //System.out.println(index);
        //把牌的索引分配给玩家和底牌
        for (int i = 0; i < index.size() - 3; i++) {
            if (i % 3 == 0) {
                one.add(index.get(i));
            }
            if (i % 3 == 1) {
                two.add(index.get(i));
            }
            if (i % 3 == 2) {
                three.add(index.get(i));
            }
        }
        //最后三张分配给底牌
        dipai.add(index.get(index.size() - 1));
        dipai.add(index.get(index.size() - 2));
        dipai.add(index.get(index.size() - 3));
        //System.out.println(one);
        //System.out.println(two);
        //System.out.println(three);
        //System.out.println(dipai);


        //对玩家和底牌进行索引排序
        Collections.sort(one, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return (o2 - o1);
            }
        });
        Collections.sort(two, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        Collections.sort(three, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        Collections.sort(dipai, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        //System.out.println(one);
        //System.out.println(two);
        //System.out.println(three);
        //System.out.println(dipai);
        //根据索引找到相应的牌
        //定义一个输出牌的方法
        look("lee", map, one);
        look("ivy", map, two);
        look("jenny", map, three);
        look("底牌", map, dipai);
    }

    public static void look(String name, Map map, List list) {
        System.out.print(name + ":");
        for (Object o : list) {
            //根据玩家有的索引在map中找到牌
            System.out.print(map.get(o) + "  ");
        }
        //输出玩一个玩家就换行
        System.out.println();
    }
}
